<template>
  <div class="scolls">
      <div class="items"
       v-for="item of letters"
      :key="item" :ref="item"
      @touchstart="headletouchstart"
      @touchmove="headletouchmove"
      @touchend="headletouchend"
      @click="headlelatter">
          {{item}}
      </div>
  </div>
</template>

<script>
export default {
  name: 'CityAplah',
  props: {
    aplahlist: Array
  },
  data () {
    return {
      touchStatus: false,
      starttop: 0,
      timer: null
    }
  },
  updated () {
    this.starttop = this.$refs['A'][0].offsetTop
  },
  methods: {
    headlelatter (e) {
      this.$emit('changeheadle', e.target.innerText)
    },
    headletouchstart () {
      this.touchStatus = true
    },
    headletouchmove (e) {
      if (this.touchStatus) {
        // const starttop = this.$refs['A'][0].offsetTop
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const movetop = e.touches[0].clientY - 77
          const index = Math.floor((movetop - this.starttop) / 20)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('changeheadle', this.letters[index])
          }
        }, 20)
      }
    },
    headletouchend () {
      this.touchStatus = false
    }
  },
  computed: {
    letters () {
      const letters = []
      this.aplahlist.forEach(i => {
        letters.push(i.title)
      })
      return letters
    }
  }
}
</script>

<style lang="scss">
  .scolls{
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: absolute;
      top: 1.6rem;
      right: .1rem;
      bottom: 0;
      width: .4rem;
      text-align: center;
      .items{
          line-height: .4rem;
          color: #00bcd4;
      }
  }
</style>
